package com.zw.controller;

import com.zw.feign.TorderInfoFeign;
import com.zw.pojo.TOrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.*;

/**
 * 订单管理表;主要保存总订单信息，下单时间、支付状态、下单金额等(TOrderInfo)表控制层
 *
 * @author makejava
 * @since 2024-11-23 23:47:01
 */
@RestController
@RequestMapping("tOrderInfo")
public class TOrderInfoController {
    /**
     * 服务对象
     */
    /*@Autowired
    private RestTemplate restTemplate;

    private static final String url = "http://springcloud-provider/";*/
    @Autowired
    private TorderInfoFeign torderInfoFeign;

    @GetMapping("/firstTest")
    public List<TOrderInfo> firstTest() {
        List<TOrderInfo> forObject = torderInfoFeign.firstTest();
        return forObject;
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(4);
        /*Future<String> submit = executorService.submit(() ->{
            TimeUnit.SECONDS.sleep(3);
            return Thread.currentThread().getName()+"===>执行了第一条子线程任务";}
        );
        Future<String> submit2 = executorService.submit(() ->{
            System.out.println(11111);
            TimeUnit.SECONDS.sleep(5);
            return Thread.currentThread().getName()+"===>执行了第二条子线程任务";});
        Future<String> submit3 =executorService.submit(() ->Thread.currentThread().getName()+"===>执行了第三条子线程任务");
        Future<String> submit4 = executorService.submit(() ->Thread.currentThread().getName()+"===>执行了第四条子线程任务");
        System.out.println(submit.get());
        System.out.println(submit2.get());
        System.out.println(submit3.get());
        System.out.println(submit4.get());
        System.out.println(Thread.currentThread().getName()+"====》main线程开始执行任务");
        executorService.shutdown();*/
        CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "ok1";});
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
            return "ok2";});
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->{
            return "ok3";});
        // whenComplete方法：在任务完成时执行回调，无论成功或失败
        future.whenComplete((result, ex) -> {
            if (ex == null) {
                System.out.println("Result: " + result);
            } else {
                System.out.println("Exception: " + ex.getMessage());
            }
        });

// handle方法：在任务完成时执行回调，无论成功或失败，并可以修改结果
        future1.handle((result, ex) -> {
            if (ex == null) {
                return result.toUpperCase();
            } else {
                return "Error: " + ex.getMessage();
            }
        }).thenAccept(System.out::println);
        System.out.println(Thread.currentThread().getName()+"====》main线程开始执行任务");

    }
}

